package kr.pe.johnkim.waiting.mobile.controller;

import kr.pe.johnkim.waiting.mobile.domain.WaitingMerchant;
import kr.pe.johnkim.waiting.mobile.service.WaitingMerchantService;
import kr.pe.johnkim.waiting.mobile.util.WaitingCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Waiting 가맹점 관련 controller 클래스 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 5. 23.
 */
@Controller
@RequestMapping(value = "/waitingMerchantController")
public class WaitingMerchantController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(WaitingMerchantController.class);

    @Autowired
    private WaitingMerchantService waitingMerchantService;

    @Autowired
    private WaitingCommon waitingCommon;

    /**
     * 가맹점 정보를 가져 온다.
     *
     * @param waitingMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectMerchantInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMerchant selectMerchantInformation(@RequestBody WaitingMerchant waitingMerchant) throws Exception {
        logger.info("selectMerchantInformation");

        logger.info("merchantId : {}", waitingMerchant.getMerchantId());
        logger.info("activateYn : {}", waitingMerchant.getActivateYn());

        return this.waitingMerchantService.selectMerchantInformation(waitingMerchant);
    }

    /**
     * 가맹점 로그인을 한다.
     *
     * @param waitingMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMerchant login(@RequestBody WaitingMerchant waitingMerchant) throws Exception {
        logger.info("login");

        // 비밀번호를 비교하기 위해서 암호화 한다.
        waitingMerchant.setPassword(waitingCommon.getEncodedPassword(waitingMerchant.getPassword()));

        // 현재 날짜를 세팅 한다.
        waitingMerchant.setCurrentDate(waitingCommon.getCurrentDate());

        logger.info("merchantId    : {}", waitingMerchant.getMerchantId());
        logger.info("password      : {}", waitingMerchant.getPassword());
        logger.info("restrictionYn : {}", waitingMerchant.getRestrictionYn());
        logger.info("activateYn    : {}", waitingMerchant.getActivateYn());

        return this.waitingMerchantService.login(waitingMerchant);
    }
}
